package com.Dao.Imp;

import com.Dao.UpdateUserDao;
import com.Utils.C3P0Utils;
import com.pojo.User;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.dbutils.QueryRunner;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
@Service
@Slf4j
public class UpdateUserDaoImp implements UpdateUserDao {
    @Override
    public int update(String username, String password) throws SQLException {
        QueryRunner queryRunner = new QueryRunner(C3P0Utils.getDataSource());
        String sql = "update userlist set password=? where username=?";
        int is_update = queryRunner.update(sql, password, username);
        if (is_update < 0) {//账户不存在
            return -1;
        }
        return 1;
    }

    @Override
    public void update(int id,String username,String password) throws SQLException {
        QueryRunner queryRunner = new QueryRunner(C3P0Utils.getDataSource());
        String sql = "update userlist set password=? where id=?";
        id=id+1;
        queryRunner.update(sql,password,id);
    }

    @Override
    public void updateStatus(int id,int status) throws SQLException {
        QueryRunner queryRunner=new QueryRunner(C3P0Utils.getDataSource());
        String sql="update userlist set status=? where id=?";
        queryRunner.update(sql,status,id);
    }

    @Override
    public void updateNickName(String username, String nickName) throws SQLException {
        QueryRunner queryRunner=new QueryRunner(C3P0Utils.getDataSource());
        String sql="update userlist set nickname=? where username=?";
        queryRunner.update(sql,nickName,username);
    }
}
